//fieldpylon.txt - This is part of a field of pylons which all explode when someone gets
//  close. They then recharge themselves eventually, unless the control turning them off is flipped.
// The pylons are set off by sending message 105.
//Cell 0 - The number of d8 damage it does.
//Cell 1,2 - A stuff done flag. If > 0, this mine has been deactivated or was set off
//Cell 3 - This is the number of the previous pylon in the chain, 0 if none.
//Cell 4 - This is the number of the next pylon in the chain, 0 if none.
// Cell 5 - If 1, triggered by any friendly character

beginobjectscript; // mine

variables;
short near_char;
short r1;
short icon_off = 0;
short i;
short ive_gone_off = 0;
short wisp_counter = 0;
short turn_on_counter = 0;
short cur_tick;
short cur_tick2;

body;

beginstate INIT_STATE;
	cur_tick = get_current_tick();
	cur_tick2 = get_current_tick();
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; 
	// turn mine off if flag set
	if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0)) {
		if (get_sdf(get_memory_cell(1),get_memory_cell(2)) > 0) {
			if (icon_off == 0)
				set_object_icon(ME,6);
			icon_off = 1;
			end();
			}
		}
	
	// at this point, if mine is off, turn it back on.
	if (icon_off == 1) {
		icon_off = 0;
		set_object_icon(ME,2);
		}
		
	// if it's off, start turning it back on
	if ((ive_gone_off > 0) && (cur_tick != get_current_tick())) {
		cur_tick = get_current_tick();
		turn_on_counter = turn_on_counter - 1;
		if (turn_on_counter == 0) {
			ive_gone_off = 0;
			set_object_icon(ME,2);
			if (dist_to_pc() <= 8)
				print_str("You hear a pylon charging back up.");
			}
	
		end();
		}
	if (ive_gone_off > 0)
		end();
			
	// fire wisps	
	wisp_counter = wisp_counter + 1;
	if (wisp_counter > 1)
		wisp_counter = 0;
	if (wisp_counter == 1) {
		if (get_memory_cell(3) > 0)
			shoot_projectile(ME,1000 + get_memory_cell(3),34);
		if (get_memory_cell(4) > 0)
			shoot_projectile(ME,1000 + get_memory_cell(4),34);
		}

	if (cur_tick2 != get_current_tick()) {
		cur_tick2 = get_current_tick();
		create_missile_spiral(158,6,2,2);
		}
		
	// text bubble
	if (get_ran(1,0,8) == 4)
		create_text_bubble("Zzzzzt!");

		
	near_char = -1;	
	if (dist_to_pc() <= 2)
		near_char = pc_num();

	if (near_char >= 0) { // check to make sure pc visible
		if (can_detect_pc(4) == FALSE)
			near_char = -1;
		}
	if ((get_memory_cell(5) > 0) && (near_char < 0)) {
		near_char = get_nearest_good_char(2);
		}
		
	if ((near_char >= 0) || (my_current_message() == 105)) {
		pc_heard_sound(238); 
		run_sparkles_on_object(ME,177,1,4);
		r1 = get_ran(get_memory_cell(0),1,24);
		if (get_memory_cell(0) >= 100)
			r1 = r1 + 2500;
			else if (get_memory_cell(0) >= 40)
				r1 = r1 + 1000;
  		create_missile_spiral(155,20,8,2);
		damage_nearby(r1,8,1,0);
		ive_gone_off = 1;
		cur_tick = get_current_tick();
		turn_on_counter = 30;
		set_object_icon(ME,6);
		
		if (near_char >= 0) {
			if (get_memory_cell(3) > 0) {
				give_object_message(get_memory_cell(3),105);
				}
			if (get_memory_cell(4) > 0) {
				give_object_message(get_memory_cell(4),105);
				}
			}
		}

break;

beginstate 3;
	
break;